package com.zy.design.demo.behavioral;

/**
 * 责任链模式
 * 特点：请求在链条中传递，直到被某个处理者处理。
 */
abstract class Handler {
    protected Handler next;

    public void setNext(Handler next) { this.next = next; }
    public abstract void handleRequest(int request);
}

class ConcreteHandlerA extends Handler {
    @Override
    public void handleRequest(int request) {
        if (request < 10) {
            System.out.println("A处理了请求：" + request);
        } else if (next != null) {
            next.handleRequest(request);
        }
    }
}

class ConcreteHandlerB extends Handler {
    @Override
    public void handleRequest(int request) {
        System.out.println("B处理了请求：" + request);
    }
}

class Main6 {
    public static void main(String[] args) {
        Handler handlerA = new ConcreteHandlerA();
        Handler handlerB = new ConcreteHandlerB();
        handlerA.setNext(handlerB);

        handlerA.handleRequest(5);
        handlerA.handleRequest(20);
    }
}
